package com.greedy.bulls.stock.user.controller;

import com.greedy.bulls.stock.common.exception.ComponentException;
import com.greedy.bulls.stock.common.exception.constants.ApplicationErrorCodeEnum;
import com.greedy.bulls.stock.entity.user.TradeUser;
import com.greedy.bulls.stock.entity.user.TradeUserFile;
import com.greedy.bulls.stock.user.controller.base.BaseController;
import com.greedy.bulls.stock.user.minio.MinioOperator;
import com.greedy.bulls.stock.user.service.IStockUserFileService;
import com.greedy.bulls.stock.user.service.IStockUserService;
import com.greedy.stock.common.web.vo.ApiRespResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;

/**
 * @Author: Yanxy
 * @Date: Created in:2023-07-29
 * Description: 用户服务控制器StockUserController
 */
@RestController()
@RequestMapping("/user")
@Log4j2
@Validated
public class StockUserController extends BaseController {

    @Autowired
    private IStockUserService stockUserService;

    @Autowired
    private IStockUserFileService stockUserFileService;

    @Autowired
    private MinioOperator minioOperator;

    /**
     * 用户登陆接口
     *
     * @param userNo  用户账号
     * @param userPwd 用户密码
     * @return 用户对象
     */
    @RequestMapping("/userLogin")
    public ApiRespResult userLogin(@RequestParam("userNo") String userNo, @RequestParam("userPwd") String userPwd) {
        ApiRespResult result;
        try {
            // 用户登陆逻辑处理
            TradeUser tradeUser = stockUserService.userLogin(userNo, userPwd);
            result = ApiRespResult.success(tradeUser);
        } catch (ComponentException e) {
            log.error(e.getMessage(), e);
            result = ApiRespResult.error(e.getErrorCodeEnum());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result = ApiRespResult.sysError(e.getMessage());
        }
        return result;
    }

    /**
     * 上传用户身份证
     * @param file 上传文件
     * @return 用户文件ID
     */
    @PostMapping("/uploadIdCard")
    public ApiRespResult uploadIdCard(@RequestParam("file") MultipartFile file) {
        ApiRespResult result;
        try {
            // 获取用户ID
            Long userId = getUserId();
            // 保存用户上传文件
            String userFileId = stockUserFileService.uploadUserIdCard(userId, file);
            result = ApiRespResult.success(userFileId);
        } catch (ComponentException e) {
            log.error(e.getMessage(), e);
            result = ApiRespResult.error(e.getErrorCodeEnum());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result = ApiRespResult.sysError(e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/downloadFile", method = {RequestMethod.GET, RequestMethod.POST})
    public ApiRespResult downloadFile(@NotBlank(message = "文件ID不能为空！") @Param("fileId") String fileId, HttpServletResponse response) throws ComponentException {
        // 获取用户文件
        TradeUserFile tradeUserFile = stockUserFileService.getTradeUserFile(fileId);
        if (null == tradeUserFile) {
            return ApiRespResult.error(ApplicationErrorCodeEnum.USER_FILE_NOT_FOUND);
        }
        minioOperator.downloadFile(response, fileId, null);
        return ApiRespResult.success();
    }

}